/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.transformer.service.impl;

import me.zhengjie.modules.transformer.domain.FdsData;
import me.zhengjie.modules.transformer.query.mapper.MessageMapper;
import me.zhengjie.modules.transformer.query.pojo.FdsRecord;
import me.zhengjie.modules.transformer.query.service.RecordService;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.transformer.repository.FdsDataRepository;
import me.zhengjie.modules.transformer.service.FdsDataService;
import me.zhengjie.modules.transformer.service.dto.FdsDataDto;
import me.zhengjie.modules.transformer.service.dto.FdsDataQueryCriteria;
import me.zhengjie.modules.transformer.service.mapstruct.FdsDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;

import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
 * @author RuitaoWei
 * @website https://el-admin.vip
 * @description 服务实现
 * @date 2021-11-30
 **/
@Service
@RequiredArgsConstructor
public class FdsDataServiceImpl implements FdsDataService {

    private final FdsDataRepository fdsDataRepository;
    private final FdsDataMapper fdsDataMapper;

    @Autowired
    MessageMapper messageMapper;

    @Autowired
    RecordService recordService;

    @Override
    public Map<String, Object> queryAll(FdsDataQueryCriteria criteria, Pageable pageable) {
        Page<FdsData> page = fdsDataRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(fdsDataMapper::toDto));
    }

    @Override
    public List<FdsDataDto> queryAll(FdsDataQueryCriteria criteria) {
        return fdsDataMapper.toDto(fdsDataRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }

    @Override
    @Transactional
    public FdsDataDto findById(Integer id) {
        FdsData fdsData = fdsDataRepository.findById(id).orElseGet(FdsData::new);
        ValidationUtil.isNull(fdsData.getId(), "FdsData", "id", id);
        return fdsDataMapper.toDto(fdsData);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public FdsDataDto create(FdsData resources) {
        return fdsDataMapper.toDto(fdsDataRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<FdsDataDto> create(List<FdsData> data) {
        //存入record表
        recordService.storeRecord(data.get(0).getRecordId());

        List<FdsDataDto> dtos = new ArrayList<>();
        for (FdsData datum : data) {
            dtos.add(create(datum));
        }
        return dtos;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(FdsData resources) {
        FdsData fdsData = fdsDataRepository.findById(resources.getId()).orElseGet(FdsData::new);
        ValidationUtil.isNull(fdsData.getId(), "FdsData", "id", resources.getId());
        fdsData.copy(resources);
        fdsDataRepository.save(fdsData);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            fdsDataRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<FdsDataDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (FdsDataDto fdsData : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("变压器ID", fdsData.getEquipId());
            map.put("导入批次", fdsData.getRecordId());
            map.put("介质损耗因数", fdsData.getTanDelta());
            map.put("复介电常数实部", fdsData.getEReal());
            map.put("复介电常数虚部", fdsData.getEImaginary());
            map.put("复电容实部", fdsData.getCReal());
            map.put("复电容虚部", fdsData.getCImaginary());
            map.put("创建人", fdsData.getCreateBy());
            map.put("更新人", fdsData.getUpdateBy());
            map.put("创建时间", fdsData.getCreateTime());
            map.put("更新时间", fdsData.getUpdateTime());
            map.put("频率", fdsData.getFrequency());
            map.put("功率因数", fdsData.getPowerFactor());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}